//
//  KAAddIconViewController.m
//  KeepAccounts
//
//  Created by honeyrr on 2017/10/18.
//  Copyright © 2017年 honeyrr. All rights reserved.
//

#import "KAEditItemViewController.h"
#import "KACustomItemViewController.h"

@interface KAEditItemViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *allItemTableView;
@property(nonatomic, strong) NSArray *itemArr;

@property(nonatomic, copy) NSString *plistPath;
@end

@implementation KAEditItemViewController

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    self.itemArr = [[KAReadFile sharedInstance] readPlistFile:self.plistPath];
    [self.allItemTableView reloadData];
}

-(NSArray *)itemArr
{
    if (!_itemArr) {
        _itemArr = [NSArray array];
    }
    return _itemArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"2");
    self.navigationItem.title = [NSString stringWithFormat:@"编辑%@分类",self.billType];
    if ([self.billType isEqualToString:@"支出"]) {
        self.plistPath = KAOutlayPlistPath;
    } else {
        self.plistPath = KAIncomePlistPath;
    }
    self.allItemTableView.tableFooterView = [UIView new];
    [self setNavBtn];
}

-(void) setNavBtn
{
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"添加" style:UIBarButtonItemStylePlain target:self action:@selector(addIconItem)];
}

-(void) addIconItem
{
    KACustomItemViewController *customItem = [KACustomItemViewController new];
    customItem.billType = self.billType;
    [self.navigationController pushViewController:customItem animated:YES];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.itemArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *const editCell = @"editCell";
    UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:editCell];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:editCell];
    }
    KABillItem *billItem = self.itemArr[indexPath.row];
    cell.imageView.image = [UIImage imageNamed:billItem.imageName];
    cell.textLabel.text = billItem.titleName;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 52;
}

@end
